﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFEntity;
using Iservices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Model;
using Newtonsoft.Json;
using Services;

namespace Hr.Controllers
{
    public class diaodongguanliController : Controller
    {
        private readonly ILogger<diaodongguanliController> _logger;
        private readonly Ifirst_kindServices fdServices;
        private readonly Ithird_kindServices tkServices;
        private readonly Isecond_kindServices skServices;
        private readonly Imajor_changeServices imajor;
        private readonly MyDbContext myDbContext;
        private readonly Iconfig_major_kindServices KindServices;
        private readonly Iconfig_majorServices iconfig_Major;
        private readonly Isalary_standardServices xcsz;
        private readonly Ihuman_registerServices rl;
        private readonly Ihuman_fileServices rl2;
        public diaodongguanliController(MyDbContext myDbContext, Imajor_changeServices imajor, ILogger<diaodongguanliController> logger, Ifirst_kindServices fdServices,
            Isecond_kindServices skServices, Ithird_kindServices tkServices, Iconfig_major_kindServices kindServices, Iconfig_majorServices iconfig_Major,
            Isalary_standardServices xcsz,Ihuman_registerServices rl,Ihuman_fileServices rl2)
        {
            this.myDbContext = myDbContext;
            _logger = logger;
            this.fdServices = fdServices;
            this.skServices = skServices;
            this.tkServices = tkServices;
            this.imajor = imajor;
            this.KindServices = kindServices;
            this.iconfig_Major = iconfig_Major;
            this.xcsz = xcsz;
            this.rl=rl;
            this.rl2 = rl2;
        }
        [HttpGet]
        public IActionResult register_locate()
        {
            return View();
        }
        [HttpPost]
        //一级
        public async Task<ActionResult> one()
        {
            List<config_file_first_kind> first = await fdServices.selectAll();
            return Ok(first);
        }
        [HttpPost]
        //二级
        public async Task<ActionResult> two(string t)
        {
            List<config_file_second_kind> second = await skServices.selectAll();
            List<config_file_second_kind> two = second.Where(e => e.first_kind_id == t).ToList();
            return Content(JsonConvert.SerializeObject(two));
        }
        [HttpPost]
        //三级
        public async Task<ActionResult> three(string t)
        {
            List<config_file_third_kind> second = await tkServices.selectAll();
            List<config_file_third_kind> three = second.Where(e => e.second_kind_id == t).ToList();
            return Content(JsonConvert.SerializeObject(three));
        }
        //调动登记列表 
        [HttpGet]
        public IActionResult register_list(string first_kind_id, string second_kind_id, string third_kind_id, DateTime start, DateTime end)
        {
            ViewBag.first_kind_id = first_kind_id;
            ViewBag.second_kind_id = second_kind_id;
            ViewBag.third_kind_id = third_kind_id;
            ViewBag.start = start;
            ViewBag.end = end;
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> register_list1(int currentpage, string first_kind_id,string second_kind_id,string third_kind_id,DateTime start,DateTime end)
        {
         
                //or regist_time  between '2020-10-30 00:00:00' and '2020-11-11 00:00:00'
                string where = $@"regist_time  between '{start}' and '{end}'" +
                    $"and (first_kind_id = '{first_kind_id}' and  second_kind_id = '{second_kind_id}' or  third_kind_id = '{third_kind_id}')";
                FenYe ye = new FenYe()
                {
                    CurrentPage = currentpage,
                    PageSize = 3,
                    Where = where
                };
                Dictionary<string, object> di = new Dictionary<string, object>();
                List<human_file> list = await rl.Humman_registerSlectFY(ye);
                di["dt"] = list;
                di["rows"] = ye.Rows;
                di["pages"] = ye.Pages;
                return Ok(di);
            
        }
        //职位分类
        public async Task<IActionResult> GetZwfl()
        {
            List<config_major_kind> config_Major_Kinds = await KindServices.config_major_kindSelect();//职位分类                            
            return Ok(config_Major_Kinds);
        }
        //职位名称设置
        public async Task<IActionResult> GetZwmc(string z)
        {
            List<config_major> config_Major = await iconfig_Major.config_majorSelect();//职位分类                            
            List<config_major> cm = config_Major.Where(e => e.major_kind_id == z).ToList();
            return Content(JsonConvert.SerializeObject(cm));
        }
        //薪酬设置

        public async Task<IActionResult> GetXcsz()
        {
            List<salary_standard> ssd = await xcsz.Select();//职位分类                            
            return Ok(ssd);
        }
        //新薪酬总额
        [HttpPost]
        public async Task<IActionResult> GetNxc(string xc)
        {
          salary_standard ss = await xcsz.GetSelId(xc);         
            return Content(Convert.ToString(ss.salary_sum));
        }
        //调动登记
        [HttpGet]
        public IActionResult register(int id)
        {
            ViewData["id"] = id;
            return View();
        }
        //查找，
        [HttpPost]
        public async Task<ActionResult> register1(int id)
        {
            var fd = await myDbContext.human_file.FindAsync(id);
            return Content(JsonConvert.SerializeObject(fd));
        }
        //登记新1级,2级,3级结构,职位分类,职位名称设置,薪酬标准
        [HttpPost]
        public async Task<IActionResult> register(Emajor_change test)
        {
                      
            int result = 0;           
                myDbContext.major_change.Add(test);
               test.check_status = 1;          
                result = await myDbContext.SaveChangesAsync();
                _logger.LogInformation("成功");                                         
            return Content(result.ToString());
        }
        [HttpGet]
        //调动成功,等待复核
        public IActionResult register_success()
        {
            return View();
        }
        [HttpGet]
        public IActionResult check_list()
        {
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> check_list(int currentpage)
        {
            FenYe ye = new FenYe()
            {
                CurrentPage = currentpage,
                PageSize = 3,
                Where = ""
            };
            Dictionary<string, object> di = new Dictionary<string, object>();
            List<major_change> list = await imajor.major_changeFenYe(ye);
            di["dt"] = list;
            di["rows"] = ye.Rows;
            di["pages"] = ye.Pages;
            return Ok(di);
        }
        [HttpGet] 
        public IActionResult check(int id)
        {
            ViewData["id"] = id;
            return View();
        }
        //查找，
        [HttpPost]
        public async Task<ActionResult> check1(int id)
        {
            var fd = await myDbContext.major_change.FindAsync(id);
            return Content(JsonConvert.SerializeObject(fd));
        }
      
        //调动新1级,2级,3级结构,职位分类,职位名称设置,薪酬标准更新人力资源
        [HttpPost]
        public async Task<  IActionResult> check(Ehuman_file test,Emajor_change ec)
        {
           
            List<major_change> mc = await imajor.major_changeselectAll();
            major_change mh = mc.Where(e => e.human_id ==test.human_id).FirstOrDefault();
            string checker = ec.checker;//审核人
            string ckreason = ec.check_reason;//审核原因
             int status=ec.check_status = 0;//状态
            int mid = mh.mch_id;//主键
            string check_time =Convert.ToString(ec.check_time);
            string sqll = string.Format("update major_change set check_reason='{0}',checker='{1}',check_status='{2}',check_time='{3}' where mch_id='{4}'", ckreason,checker,status,check_time,mid);
            int num = SqlHelper.ZSG(sqll, "diaodongguanliController");
            int result = 0;
            if (num > 0)
            {
                List<human_file> list = await rl2.Select();
                human_file fh = list.Where(e => e.human_id == test.human_id).FirstOrDefault();
                int idd = fh.huf_id;
             //   fh.major_change_amount = fh.major_change_amount + 1;
                string sql = string.Format("update human_file set first_kind_id='{0}',first_kind_name='{1}',second_kind_id='{2}',second_kind_name='{3}'," +
                    "third_kind_id='{4}',third_kind_name='{5}',human_major_kind_id='{6}',human_major_kind_name='{7}',human_major_id='{8}',hunma_major_name='{9}'," +
                    "salary_standard_id='{10}',salary_standard_name='{11}',salary_sum='{12}' where huf_id='{13}'",
                    test.first_kind_id, test.first_kind_name, test.second_kind_id, test.second_kind_name, test.third_kind_id, test.third_kind_name,
                    test.human_major_kind_id, test.human_major_kind_name, test.human_major_id, test.hunma_major_name, test.salary_standard_id,
                    test.salary_standard_name, test.salary_sum, idd);
               result = SqlHelper.ZSG(sql, "diaodongguanliController");
            }

            return Content(JsonConvert.SerializeObject(result));

        }
        [HttpGet]
        //复核成功
        public IActionResult check_success()
        {
            return View();  
        }
        [HttpGet]
        public IActionResult locate()
        {
            return View();
        }

        //调动查询 
        [HttpGet]
        public IActionResult list(string one, string two,string three,string zwfl,string zwmc, DateTime start, DateTime end)
        {           
            ViewBag.one = one;
            ViewBag.two = two;
            ViewBag.three = three;
            ViewBag.zwfl = zwfl; 
            ViewBag.zwmc = zwmc;
            ViewBag.start = start;
            ViewBag.end = end;
            return View();
        }
        //调动查询列表
        [HttpPost]
        public IActionResult list1(string one, string two, string three, string zwfl, string zwmc, DateTime start, DateTime end)
        {
         
            var list = myDbContext.major_change.Where(e =>e.check_status==0 && e.first_kind_id == one || e.second_kind_id == two && e.third_kind_id == three && e.major_kind_id == zwfl && e.major_id == zwmc ||e.regist_time>=start  &&e.regist_time<=end ).Select(e => e).ToList();
            return Ok(list);
        }
        [HttpGet]
        public IActionResult detail(int id)
        {
            ViewBag.id=id;
            return View();
        }
        
        [HttpGet]
        //薪酬标准登记查询
        public async Task<IActionResult> detail1(int id)
        {
            var fd = await myDbContext.major_change.FindAsync(id);
            return Content(JsonConvert.SerializeObject(fd));
        }
    }
}
